package com.freedy.concurrent.Monitors.patterns;

import com.freedy.concurrent.Monitors.Sleeper;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.concurrent.locks.LockSupport;

/**
 * @author Freedy
 * @date 2021/4/23 8:48
 */
@Slf4j
public class SequenceControlAlternatelyParkUnpark {
    private static Thread t1;
    private static Thread t2;
    private static Thread t3;

    public static void main(String[] args) {
        t1 = new Thread(() -> print("A", t2), "T1");
        t2 = new Thread(() -> print("B", t3), "T2");
        t3 = new Thread(() -> print("C", t1), "T3");

        t1.start();
        t2.start();
        t3.start();
        Sleeper.sleep(0.1);
        LockSupport.unpark(t1);
    }

    public static void print(String str, Thread next) {
        for (int i = 0; i < 3; i++) {
            LockSupport.park();
            log.info(str);
            LockSupport.unpark(next);
        }
    }

}
